package com.byl.modules.sys.controller;

import com.byl.common.modules.controller.BaseController;
import com.byl.modules.sys.service.MenuService;
import com.byl.sdk.bean.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/7/24.
 */
@RestController
@RequestMapping("/api/sys/menu")
public class MenuController extends BaseController {

    @Autowired
    MenuService menuService;

    @PostMapping("/save")
    public String saveMenu(@RequestBody Map map) {
        int rs = menuService.save(map);
        if (rs > 0)
            return success("保存成功", "");
        return fail("保存失败");
    }

    @PostMapping("/update")
    public String updateMenu(@RequestBody Map map) {
        int rs = menuService.update(map);
        if (rs > 0)
            return success("更新成功", "");
        return fail("更新失败");
    }

    @GetMapping("/delete")
    public String deleteMenu(String id) {
        String rs = menuService.deleteMenu(id);
        if ("success".equals(rs)) {
            return success("删除成功", "");
        } else if ("fail".equals(rs)) {
            return fail("删除失败");
        } else {
            return fail("删除失败，角色：" + rs + "拥有这些权限");
        }
    }

    @PostMapping("/list")
    public String queryMenu(@RequestBody Map map) {
        List list = menuService.queryMenuTreeTable();
        return success("查询结果", list);
    }

    @GetMapping("/tree")
    public String queryMenuTree() {
        List list = menuService.queryMenuTree();
        return success("菜单树", list);
    }

    @GetMapping("/auth/tree")
    public String queryAuthMenuTree() {
        List list = menuService.queryAuthMenuTree();
        return success("功能授权", list);
    }

    @GetMapping("/get")
    public String getMenu(String id) {
        Map map = menuService.getMapById(id);
        return success("菜单详情", map);
    }

    /**
     * 获取登录权限
     *
     * @return
     */
    @GetMapping("/nav")
    public String getNav(String userId) {
        Map map = menuService.getNav(userId);
        return success("权限", map);
    }

}
